package com.zj.printdemo;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.speech.RecognizerResultsIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.yyr.bluetoothpair.BluetoothPairService;
import com.yyr.bluetoothpair.ClsUtils;

import cn.hlife.com.bluetooth20_connect.util.LogUtils;

public class CustomBluetoothFrame extends Activity {
    int REQUEST_ENABLE = 202;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_bluetooth_frame);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//搜索发现设备
        registerReceiver(new BluetoothReciver(), intentFilter);
        findViewById(R.id.search_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BluetoothPairService.getBluetoothAdapter().startDiscovery();

            }
        });
        findViewById(R.id.print).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (BluetoothPairService.isBluetoothEnabled()) {
                    LogUtils.e("已打开");
                } else {
                    //弹出对话框提示用户是后打开
                    Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enabler, REQUEST_ENABLE);
                    //直接打开
//                    BluetoothPairService.openBluetooth();
                    LogUtils.e("未打开");
                    IntentFilter intentFilter = new IntentFilter();
                    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
                    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//搜索发现设备
                    registerReceiver(new BluetoothReciver(), intentFilter);
                }
                ;
            }
        });
    }

    public class BluetoothReciver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        LogUtils.e("STATE_OFF 手机蓝牙关闭");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        LogUtils.e("STATE_TURNING_OFF 手机蓝牙正在关闭");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        LogUtils.e("STATE_ON 手机蓝牙开启");
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        LogUtils.e("STATE_TURNING_ON 手机蓝牙正在开启");
                        break;
                }
            }
//            if (BluetoothDevice.ACTION_FOUND.equals(action)) {  //发现设备
//                //获取蓝牙设备
//                BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//                LogUtils.e("发现设备:[" + btDevice.getName() + "]" + ":" + btDevice.getAddress());
//
//                if (btDevice.getName().contains("HC-05")){//HC-05设备如果有多个，第一个搜到的那个会被尝试。
//
//                    if (btDevice.getBondState() == BluetoothDevice.BOND_NONE) {
//
//                        LogUtils.e("attemp to bond:" + "[" + btDevice.getName() + "]");
//                        try {
//                            //通过工具类ClsUtils,调用createBond方法
//                            ClsUtils.createBond(btDevice.getClass(), btDevice);
//                        } catch (Exception e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//                    }
//                } else
//                    LogUtils.e("error  Is faild");
//            } else if (action.equals("android.bluetooth.device.action.PAIRING_REQUEST")){ //再次得到的action，会等于PAIRING_REQUEST
//
//                LogUtils.e("action2=" + action);
//                if (btDevice.getName().contains("HC-05")) {
//                    LogUtils.e("here OKOKOK");
//
//                    try {
//
//                        //1.确认配对
//                        ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true);
//                        //2.终止有序广播
//                        LogUtils.e("order...  isOrderedBroadcast:" + isOrderedBroadcast() + ",isInitialStickyBroadcast:" + isInitialStickyBroadcast());
//                        abortBroadcast();//如果没有将广播终止，则会出现一个一闪而过的配对框。
//                        //3.调用setPin方法进行配对...
//                        boolean ret = ClsUtils.setPin(btDevice.getClass(), btDevice, pin);
//
//                    } catch (Exception e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                } else
//                    LogUtils.e("这个设备不是目标蓝牙设备");
//
//            }
        }
    }


}
